﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KorleBu.Application_Classes;

namespace KorleBu
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        #region Class State Variables

        /// <summary>
        /// Dataset object, used for housing login method calls results.
        /// </summary>
        DataSet ds = new DataSet();
        /// <summary>
        /// Presentation object that accesses the presentation object classes methods.
        /// Used for accessing business methods for the logging out.
        /// </summary>
        Presentation presentation = new Presentation();

        #endregion

        #region Methods

        /// <summary>
        /// Loads up the home page, but first checks if the user browsed to this page without logging in first.
        /// Depending on the user's role, a message might be shown in the side bar stating whether they have pending tasks to do.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //check if the session is valid still, if the results return a message return them back to the log in screen.
            ds = presentation.IsValidSession(GetSessionID());

            if(!String.IsNullOrEmpty(ds.Tables[0].Rows[0][0].ToString()))
            {
                Response.Redirect("~/Login.aspx", true);
            }

            //if the user didn't log in, but browsed to this page redirect them to the login page.
            if (GetSessionID() == null || GetSessionID() == "")
            {
                Response.Redirect("~/Login.aspx", true);
            }

            //check permissions of the user and gray out any button links that they aren't permitted to access

            //requests/requisitions page
            if (!(GetIsGeneralManager() || GetIsCEO()|| GetIsStoreManager() || GetIsSubUnitClerk() || /*Global.loggedInUser.getIsStoreKeeper() || Global.loggedInUser.getIsAccounting() ||*/ GetIsProcurement()))
            {
                iBtnRequests.Visible = false;
                //iBtnRequests.ImageUrl = @"images/(hue)empty.png";
            }

            //assets page
            if (!(GetIsAdmin()))
            {
                iBtnAssets.Visible = false;
                //iBtnAssets.ImageUrl = @"images/(hue)empty.png";
            }

            //reports page
            if (!(GetIsGeneralManager() || GetIsCEO()|| GetIsStoreManager() /*|| Global.loggedInUser.getIsAccounting() || Global.loggedInUser.getIsStoreKeeper()*/))
            {
                iBtnReports.Visible = false;
                //iBtnReports.ImageUrl = @"images/(hue)assets-gray.jpg";
            }

            //users page
            if (!GetIsAdmin())
            {
                iBtnUsers.Visible = false;
                //iBtnUsers.ImageUrl = @"images/(hue)empty.png";
            }
        }

        #region Buttons

        /// <summary>
        /// Button on click method handles the routing of the user to the assets page.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void assets_Click(object sender, EventArgs e)
        {
            if (iBtnAssets.ImageUrl == @"images/(hue)assets.jpg")
                Response.Redirect("~/Items.aspx", false);
        }
        /// <summary>
        /// Button on click method handles the routing of the user to the requests page.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void requests_Click(object sender, EventArgs e)
        {
            if (iBtnRequests.ImageUrl == @"images/(hue)requests.jpg")
            {
                if (GetIsCEO()|| GetIsGeneralManager() || GetIsProcurement() /*|| Global.loggedInUser.getIsAccounting()*/)
                {
                    Response.Redirect("~/Requisitions.aspx", false);
                }
                else if (GetIsStoreManager() /*|| Global.loggedInUser.getIsStoreKeeper()*/)
                {
                    Response.Redirect("~/RequisitionStoreManager.aspx", false);
                }
                else if (GetIsSubUnitClerk())
                {
                    Response.Redirect("~/Requests.aspx", false);
                }
            }
        }
        /// <summary>
        /// Button on click method handles the routing of the user to the reports page.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void reports_Click(object sender, EventArgs e)
        {
            if (iBtnReports.ImageUrl == @"images/(hue)reports.jpg")
                Response.Redirect("~/Reports.aspx", false);
        }
        /// <summary>
        /// Button on click method handles the routing of the user to the user page.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void users_Click(object sender, EventArgs e)
        {
            if (iBtnUsers.ImageUrl == @"images/(hue)users.jpg")
                Response.Redirect("~/Users.aspx", false);
        }
        /// <summary>
        /// Method handles the Logout Button from every page.
        /// Clears out the global user object, logs out the user's presence in the database session table,
        /// and redirects the user to the login screen.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            ds = presentation.Logout(GetSessionID());
            //If there are errors with trying to log out the user, show an appropriate message, otherwise clear out 
            //the global user object, and redirect to the login page.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    //lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                }
                else
                {
                    User lg = (User)Session["LoggedInUser"];
                    lg.LogOutUser();
                    Response.Redirect("~/Login.aspx", false);
                }
            }
            //else
            //    lblMasterError.Text = "Sorry, the system could not log you out."; // If the Data Tier cannot be reached
        }
        #endregion

        #endregion

        #region Session Methods
        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }
        private string GetSessionID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getSessionId();
        }
        private bool GetIsAdmin()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAdmin();
        }
        private bool GetIsGeneralManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsGeneralManager();
        }
        private bool GetIsStoreManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreManager();
        }
        private bool GetIsStoreKeeper()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreKeeper();
        }
        private bool GetIsSubUnitClerk()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsSubUnitClerk();
        }
        private bool GetIsAccounting()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAccounting();
        }
        private bool GetIsProcurement()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsProcurement();
        }
        private bool GetIsCEO()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsCEO();
        }
        #endregion
    }
}